package org.yu.register.capability;


import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityProvider;
import net.minecraftforge.common.util.INBTSerializable;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.energy.CapabilityEnergy;
import net.minecraftforge.energy.EnergyStorage;
import org.yu.register.CapabilityRegister;

import javax.annotation.Nonnull;
import java.util.function.Supplier;

public class LingQiProvider extends CapabilityProvider<LingQiProvider> implements INBTSerializable<CompoundTag> {

    private final LazyOptional<LingQi> capability;

    public LingQiProvider(LivingEntity livingEntity) {
        super(LingQiProvider.class);
        capability = LazyOptional.of(()->new LingQi(Integer.MAX_VALUE));
    }
    public LingQiProvider(ItemStack itemStack) {
        super(LingQiProvider.class);
        capability = LazyOptional.of(()->new LingQi(Integer.MAX_VALUE));
    }

    @Nonnull
    @Override
    public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, Direction side) {
        return CapabilityRegister.LING_QI.orEmpty(cap, capability.cast());
    }

    @Override
    public CompoundTag serializeNBT() {
        CompoundTag tag=new CompoundTag();
        if (capability.resolve().isPresent())  tag.put("LingQi",capability.resolve().get().serializeNBT());
        return tag;
    }

    @Override
    public void deserializeNBT(CompoundTag nbt) {
            capability.ifPresent(cap -> cap.deserializeNBT(nbt.getCompound("LingQi")));
    }
}
